package com.seed.uaa.web.organization;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seed.core.annotation.*;
import com.seed.core.enumeration.StatType;
import com.seed.core.pojo.*;
import com.seed.uaa.pojo.OwnerAction;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "组织管理")
@RestController
@RequestMapping("organization")
@SaCheckPermission
@EntityClass(Organization.class)
public class OrganizationController {

    private final OrganizationService organizationService;

    public OrganizationController(OrganizationService service) {
        this.organizationService = service;
    }

    @Operation(summary = "分页列表")
    @GetMapping("page")
    @Declares("manager,code,name,expire_date,usable,loginable,enable,clerk_size, roles")
    @Searches("code_eq, name_like, manager.name_like")
    @Orders("id=desc, code, name, manager.name")
    @Stat(name = "stat1", label = "统计个数1", comment = "统计个数123", declare = "name")
    @Stat(name = "stat2", label = "xxx", declare = "manager.roles.organizations.name")
    @Stat(name = "stat3", label = "角色数量", declare = "roles", searches = "roles.name_like=部门经理")
    @Stat(name = "stat4", label = "启用个数", declare = "enable", searches = "enable_eq=yes")
    @Stat(name = "stat5", label = "总编制", type = StatType.sum, declare = "clerk_size", searches = "clerk_size_isNotNull")
    public R<StatPage<Organization>> page(Integer pageNumber, Integer pageSize) {
        return R.data(organizationService.page(pageNumber, pageSize));
    }

    @Operation(summary = "组织树")
    @GetMapping("lookup")
    public R<List<Node<Organization>>> lookup(boolean containLeaf) {
        return R.data(organizationService.tree(containLeaf));
    }

    @Operation(summary = "保存")
    @PostMapping("save")
    public R<Boolean> save(@RequestBody Organization organization) {
        return R.data(organizationService.save(organization));
    }

    @Operation(summary = "详情")
    @GetMapping("detail/{id}")
    @Declares("manager,code,name,expire_date,usable,loginable,enable,clerk_size, manager")
    public R<Organization> detail(@PathVariable String id) {
        return R.data(organizationService.detail(id));
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public R<Boolean> remove(@PathVariable String id) {
        return R.data(organizationService.remove(id));
    }

//    @Operation(summary = "设置编码")
//    @GetMapping("code/{id}")
//    public R<?> code(@PathVariable String id) {
//        return R.data(organizationService.get(id));
//    }
//
//    @Operation(summary = "设置编码")
//    @PutMapping("code")
//    public R<Boolean> code(@RequestBody SetDTO dto) {
//        return R.data(organizationService.setCode(dto));
//    }

    @Operation(summary = "设置名称")
    @GetMapping("name/{id}")
    public R<?> name(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "设置名称")
    @PutMapping("name")
    public R<Boolean> name(@RequestBody SetDTO dto) {
        return R.data(organizationService.set(dto));
    }

    @Operation(summary = "设置负责人")
    @GetMapping("manager/{id}")
    public R<GetVO> manager(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "设置负责人")
    @PutMapping("manager")
    public R<Boolean> manager(@RequestBody SetDTO dto) {
        return R.data(organizationService.set(dto));
    }

    @Operation(summary = "设置截止日期")
    @GetMapping("expireDate/{id}")
    public R<?> expireDate(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "设置截止日期")
    @PutMapping("expireDate")
    public R<Boolean> expireDate(@RequestBody SetDTO dto) {
        return R.data(organizationService.set(dto));
    }

    @Operation(summary = "设置编制")
    @GetMapping("clerkSize/{id}")
    public R<?> clerkSize(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "设置编制")
    @PutMapping("clerkSize")
    public R<Boolean> clerkSize(@RequestBody SetDTO dto) {
        return R.data(organizationService.setClerkSize(dto));
    }

    @Operation(summary = "设置启用")
    @GetMapping("usable/{id}")
    @Declares("id, usable, loginable, enable, reason")
    public R<Organization> usable(@PathVariable String id) {
        return R.data(organizationService.detail(id));
    }

    @Operation(summary = "设置启用")
    @PutMapping("usable")
    @Declares("usable, loginable, enable, reason")
    public R<Boolean> usable(@RequestBody Organization organization) {
        return R.data(organizationService.update(organization));
    }

    @Operation(summary = "查看角色")
    @GetMapping("getRoles/{id}")
    public R<Object> getRoles(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "获取角色")
    @GetMapping("roles/{id}")
    public R<Object> roles(@PathVariable String id) {
        return R.data(organizationService.get(id));
    }

    @Operation(summary = "分配角色")
    @PutMapping("roles")
    public R<Boolean> roles(@RequestBody SetDTO dto) {
        return R.data(organizationService.set(dto));
    }

    @Operation(summary = "查看功能权限")
    @GetMapping("getActions/{id}")
    public R<OwnerAction> getActions(@PathVariable String id) {
        return R.data(organizationService.getOwnerAction(id));
    }

    @Operation(summary = "获取功能权限")
    @GetMapping("actions/{id}")
    public R<List<String>> actions(@PathVariable String id) {
        return R.data(organizationService.getActions(id));
    }

    @Operation(summary = "分配功能权限")
    @PutMapping("actions")
    public R<Boolean> actions(@RequestBody SetDTO dto) {
        return R.data(organizationService.setActions(dto));
    }

    @Operation(summary = "获取数据权限")
    @GetMapping("datas/{id}")
    public R<List<String>> datas(@PathVariable String id, String actionPath) {
        return R.data(organizationService.getDatas(id, actionPath));
    }

    @Operation(summary = "分配数据权限")
    @PutMapping("datas")
    public R<Boolean> datas(String id, String actionPath, @RequestBody List<String> params) {
        return R.data(organizationService.setDatas(id, actionPath, params));
    }
    @Operation(summary = "获取字段权限")
    @GetMapping("fields/{id}")
    public R<Map<String, String>> fields(@PathVariable String id, String actionPath) {
        return R.data(organizationService.getFields(id, actionPath));
    }

    @Operation(summary = "分配字段权限")
    @PutMapping("fields")
    public R<Boolean> fields(String id, String actionPath, @RequestBody Map<String, String> params) {
        return R.data(organizationService.setFields(id, actionPath, params));
    }
}
